#ifndef TILE_H
#define TILE_H

#include <QImage>

class TileMap;

class Tile
{
  QImage _tile;
  TileMap* _map;
  QRgb _innerTileAverageValue;
  QRgb _neighborhoodAverageValue;

public:
  enum Direction
  {
    Left,
    Top,
    Right,
    Bottom
  };

  int _x;
  int _y;
  Tile(TileMap* map, const QImage& tile, int x, int y);
  QRgb innerTileAverageValue() const { return _innerTileAverageValue; }
  void computeNeightBorhoodAverageValue();

  bool hasLeftWall() const;
  bool hasRightWall() const;
  bool hasTopWall() const;
  bool hasBottomWall() const;

  bool hasWall(Direction d) const;
};

class TileMap
{
  Tile*** _tiles;
  int _wallWidth;

  int _width;
  int _height;

  int _tileWidth;
  int _tileHeight;

  void checkTileExits(int x, int y, QStack<Tile *> &stack, QList<QPair<int, int> > &result, Tile::Direction startExitDirection, Tile::Direction endExitDirection) const;

public:
  static int DELTA_WALL;
  static int DELTA_TILE;

  static int CONTRAST;
  static int BRIGHTNESS;


  TileMap(int tileWidth,int tileHeight, int wallWidth, const QImage& source);

  int wallWidth() const { return _wallWidth; }
  Tile* tile(int x,int y) const { return _tiles[x][y]; }

  int width() const { return _width; }
  int height() const { return _height; }

  int tileWidth() const { return _tileWidth; }
  int tileHeight() const { return _tileHeight; }

  void highlightWalls(QImage& source) const;
  void highlightExits(QImage& source) const;

  QList<QPair<int,int> > exits() const;
};



#endif // TILE_H
